package com.example.a79273.shandianban.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.config.NetConfig_DesignerInfo;
import com.example.a79273.shandianban.model.DetailBean;
import com.squareup.picasso.Picasso;

import org.xutils.HttpManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 设计师详情,传过来的id获取对应设计师的数据
 */
public class DesignerDetail extends Fragment {


    private ImageView pic_iv;
    private TextView name_tv, fansCount_tv, clickCount_tv;
    private Button focus_bt;
    private WebView mWebView;

    private Picasso picasso = Picasso.with(getActivity());
    private HttpManager httpManager = x.http();
    private String url;
    private RequestParams requestParams;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceSate) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {

    }

    private void setData() {
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            url = bundle.getString("url");
        }

        requestParams = new RequestParams(url);

        httpManager.get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                DetailBean detailBean = DetailBean.objectFromData(MyApplication.dealWithString(result));
                if (detailBean != null) {
                    DetailBean.DataBean.ListBean bean = detailBean.getData().getList();
                    picasso.load(NetConfigYouRen.ROOT_URL + bean.getDesignerpic()).into(pic_iv);
                    name_tv.setText(bean.getName());
                    fansCount_tv.setText(bean.getFans() + "\r\n粉丝数");
                    clickCount_tv.setText(bean.getHits() + "\r\n阅读数");
                    mWebView.loadData(bean.getDetailhtml(), "text/html", "UTF-8");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }


            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView(View view) {


        pic_iv = (ImageView) view.findViewById(R.id.iv_detail_pic);
        name_tv = (TextView) view.findViewById(R.id.tv_detail_name);
        fansCount_tv = (TextView) view.findViewById(R.id.tv_detail_fansCount);
        clickCount_tv = (TextView) view.findViewById(R.id.tv_detail_clickCount);
        focus_bt = (Button) view.findViewById(R.id.bt_detail_focus);
        mWebView = (WebView) view.findViewById(R.id.wv_detail_web);
    }
}
